package com.psl.services.work.order;

import com.psl.Dao.Conn;
import com.psl.action.work.order.shippingDetails;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ShippingServices
{
  public List queryAllshipping()
  {
    Connection conn = new Conn().getConn();
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    List ship = new ArrayList();
    try
    {
      pstmt = conn.prepareStatement("SELECT c_name,declaration_number,order_id,shippingdate,receipt_date,remittance_money,unremittance_money,is_remoittance from shippingdetails");
      rs = pstmt.executeQuery();
      while (rs.next())
      {
        shippingDetails sd = new shippingDetails();
        sd.setC_name(rs.getString(1));
        sd.setDeclaration_number(rs.getString(2));
        sd.setOrder_id(rs.getString(3));
        sd.setShippingdate(rs.getString(4));
        sd.setReceipt_date(rs.getString(5));
        sd.setRemittance_money(rs.getFloat(6));
        sd.setUnremittance_money(rs.getFloat(7));
        sd.setIs_remoittance(rs.getInt(8));
        ship.add(sd);
      }
      return ship;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      return null;
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (rs != null) {
          rs.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
  }
  
  public boolean insertshipping(String c_name, String declaration_number, String order_id, String shippingdate, String receipt_date, float remittance_money, float unremittance_money, int is_remoittance)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("insert into shippingdetails(c_name,declaration_number,order_id,shippingdate,receipt_date,remittance_money,unremittance_money,is_remoittance) VALUES (?,?,?,?,?,?,?,?)");
      pstmt.setString(1, c_name);
      pstmt.setString(2, declaration_number);
      pstmt.setString(3, order_id);
      pstmt.setString(4, shippingdate);
      pstmt.setString(5, receipt_date);
      pstmt.setFloat(6, remittance_money);
      pstmt.setFloat(7, unremittance_money);
      pstmt.setInt(8, is_remoittance);
      pstmt.execute();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }
  
  public boolean updateshipping(String c_name, String declaration_number, String order_id, String shippingdate, String receipt_date, float remittance_money, float unremittance_money, int is_remoittance)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("UPDATE product set c_name=?,declaration_number=?,order_id=?,shippingdate=?,receipt_date=?,remittance_money=?,unremittance_money=?,is_remoittance=?");
      pstmt.setString(1, c_name);
      pstmt.setString(2, declaration_number);
      pstmt.setString(3, order_id);
      pstmt.setString(4, shippingdate);
      pstmt.setString(5, receipt_date);
      pstmt.setFloat(6, remittance_money);
      pstmt.setFloat(7, unremittance_money);
      pstmt.setInt(8, is_remoittance);
      pstmt.executeUpdate();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }
  
  public boolean deleteShipping(String order_id)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("delete FROM shippingdetails WHERE order_id=?");
      pstmt.setString(1, order_id);
      pstmt.execute();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }
}
